This program validates passwords to match specific rules. A valid password is one that conforms to the following rules: - Minimum length is 6; - Maximum length is 12; - Contains at least an uppercase letter or a lowercase letter - Contains at least a number; - Contains at least a special character (such as @,+,£,$,%,*^,etc); - Doesn’t contain space(s).
Prerequisites
It requires no prerequisites, you only need to run the script. If you don’t have Python installed, you can visit here to download Python.
How to run the script
Running the script is pretty easy, open a terminal in the folder where your script is located and run the following command :
import stringdef passwordValidator():""" Validates passwords to match specific rules : return: str """# display rules that a password must conform toprint('\nYour password should: ')print('\t- Have a minimum length of 6;')print('\t- Have a maximum length of 12;')print('\t- Contain at least an uppercase letter or a lowercase letter')print('\t- Contain at least a number;')print('\t- Contain at least a special character (such as @,+,£,$,%,*^,etc);')print('\t- Not contain space(s).')# get user's password userPassword =input('\nEnter a valid password: ').strip()# check if user's password conforms # to the rules aboveifnot(6<=len(userPassword) <=12): message ='Invalid Password..your password should have a minimum ' message +='length of 6 and a maximum length of 12'return messageif' 'in userPassword: message ='Invalid Password..your password shouldn\'t contain space(s)'return messageifnotany(i in string.ascii_letters for i in userPassword): message ='Invalid Password..your password should contain at least ' message +='an uppercase letter and a lowercase letter'return messageifnotany(i in string.digits for i in userPassword): message ='Invalid Password..your password should contain at least a number'return messageifnotany(i in string.punctuation for i in userPassword): message ='Invalid Password..your password should contain at least a special character'return messageelse:return'Valid Password!'my_password = passwordValidator()print(my_password)